package com.cheejee.goodscenter.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cheejee.goodscenter.module.dto.GoodsDto;
import com.cheejee.goodscenter.module.entity.Goods;
import com.cheejee.goodscenter.module.response.BaseReponse;
import com.cheejee.goodscenter.module.response.PageReponse;
import com.cheejee.goodscenter.service.GoodsBrandPropertiesService;
import com.cheejee.goodscenter.service.GoodsCategoryPropertiesService;
import com.cheejee.goodscenter.service.GoodsCustomPropertiesService;
import com.cheejee.goodscenter.service.GoodsService;
import com.cheejee.goodscenter.service.GoodsSpecificService;
import com.cheejee.goodscenter.service.SkuService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author CARRY
 * @Date 2020年10月26日下午11:15:29
 */
@RestController
@RequestMapping("/goods")
@Api(tags = "商品")
public class GoodsController {

	@Resource
	private GoodsService service;
	
	@GetMapping("/")
	@ApiOperation("查询商品列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "pageSize", value = "每页内容数", defaultValue = "0", dataType = "int"),
		@ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "0", dataType = "int") })
	public Object getAllBrand(Integer pageNum, Integer pageSize) {
		
		return PageReponse.ok(service.queryGoods(null, null, null, null, null, pageNum, pageSize));
	}
	
	@GetMapping("/order")
	@ApiOperation("查询商品列表，按添加时间排序")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "condition", value = "排序规则， true=asc， fals=desc", dataType = "boolean"),
		@ApiImplicitParam(name = "pageSize", value = "每页内容数", defaultValue = "0", dataType = "int"),
		@ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "0", dataType = "int") })
	public Object getAllBrand(Boolean condition, Integer pageNum, Integer pageSize) {
		
		return PageReponse.ok(service.queryGoodsOrderByTime(condition, pageNum, pageSize));
	}
	
	@GetMapping("/condition")
	@ApiOperation(value = "按条件查询商品列表", notes = "多个条件AND连接")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "brandId", value = "品牌ID", dataType = "int"),
		@ApiImplicitParam(name = "categoryId", value = "分类ID", dataType = "int"),
		@ApiImplicitParam(name = "goodsName", value = "商品名称(精确查询)", dataType = "string"),
		@ApiImplicitParam(name = "isOn", value = "是否上架", dataType = "boolean"),
		@ApiImplicitParam(name = "isDelete", value = "是否删除", dataType = "boolean"),
		@ApiImplicitParam(name = "pageSize", value = "每页内容数", defaultValue = "0", dataType = "int"),
		@ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "0", dataType = "int") })
	public Object getAllBrand(Integer brandId, Integer categoryId, String goodsName, Boolean isOn, Boolean isDelete, Integer pageNum, Integer pageSize) {
		return PageReponse.ok(service.queryGoods(brandId, categoryId, goodsName, isOn, isDelete, pageNum, pageSize));
	}
	
	
	@GetMapping("/{id}")
	@ApiOperation("查询商品信息")
	public Object getGoods(@PathVariable Long id) {
		
		return BaseReponse.queryOk(service.queryGoodsById(id));
	}
	
	@GetMapping("info/{id}")
	@ApiOperation(value = "查询商品信息", notes = "包含商品SKU，属性，规格信息")
	public Object getInfo(@PathVariable Long id) {
		System.out.println(service.queryGoodsInfo(id));
	
		return BaseReponse.queryOk(service.queryGoodsInfo(id));
	}
	
	@PutMapping("/")
	@ApiOperation(value = "新增商品")
	public Object addGoods(@RequestBody Goods goods) {
		service.addGoods(goods);
		
		return BaseReponse.addOk(service.queryGoodsById(goods.getId()));
	}

	@PutMapping("/info")
	@ApiOperation(value = "新增商品", notes = "同时增加商品的规格，SKU和属性信息")
	public Object addGoodsInfo(@RequestBody GoodsDto goods) {
		service.addGoodsInfo(goods);
		
		return BaseReponse.addOk(service.queryGoodsInfo(goods.getId()));
	}
	
	@PutMapping("/change")
	@ApiOperation(value = "修改商品", notes = "修改商品的基本信息")
	public Object changeGoods(@RequestBody Goods goods) {
		service.changeGoods(goods);
		
		return BaseReponse.changeOk(service.queryGoodsById(goods.getId()));
	}
	
	@PutMapping("/change/info")
	@ApiOperation(value = "修改商品", notes = "修改商品的基本信息，规格信息，SKU，属性信息。会修改SKU所包含的属性信息。如果传入的关联信息为空则不进行更新操作。")
	public Object changeGoodsInfo(@RequestBody GoodsDto goods) {
		service.changeGoodsInfo(goods);
		
		return BaseReponse.changeOk(service.queryGoodsInfo(goods.getId()));
	}
	
	@DeleteMapping("/{id}")
	@ApiOperation(value = "删除商品", notes = "将商品的删除状态设置为true")
	public Object deleteGoods(@PathVariable Long[] id) {
		service.deleteGoods(id);
		
		return BaseReponse.deleteOk();
	}
	
}
